//
//  TopicCell.swift
//  appstore
//
//  Created by llbt-sk on 2019/11/7.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class TopicCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate {
    
    let GameTopicCollectionCellIdentifier = "GameTopicCollectionCellIdentifier"
    
    let data: [GameTopicModel] = [
        GameTopicModel(name: "Bullet Hell", desc: "Casual", iconImageName: "logo_broadcast"),
        GameTopicModel(name: "Hot Wheels", desc: "Strategy", iconImageName: "logo_car"),
        GameTopicModel(name: "SpellForce - Heroes", desc: "Card", iconImageName: "logo_jump"),
        GameTopicModel(name: "Farm Punks", desc: "Role-Playing", iconImageName: "logo_smile"),
        GameTopicModel(name: "Super Spinball", desc: "A musical journey awaits", iconImageName: "logo_weibo"),
    ]
    
    var collectionView:UICollectionView!

    lazy var flowLayout:CommonCollectionFlowLayout = {
        let flowLayout = CommonCollectionFlowLayout()
        flowLayout.minimumLineSpacing = 10.0
        flowLayout.minimumInteritemSpacing = 0.0
        flowLayout.scrollDirection = .horizontal
        flowLayout.sectionInset = UIEdgeInsets(top: 0.0, left: 20.0, bottom: 0.0, right: 20.0)
        flowLayout.itemSize = CGSize(width: ScreenWidth - 40.0, height: 80.0)
        return flowLayout
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.initView()
    }
    
    func initView() -> Void {
        // headerView
        let headerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: ScreenWidth, height: 42.0))
        self.contentView.addSubview(headerView)
        
        let topicTitleLabel = UILabel()
        topicTitleLabel.font = UIFont.systemFont(ofSize: 22.0, weight: .medium)
        topicTitleLabel.textColor = UIColor.black
        topicTitleLabel.text = "What We're Playing"
        headerView.addSubview(topicTitleLabel)
        topicTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(13.0)
            make.left.equalTo(20.0)
            make.width.equalTo(200.0)
            make.height.equalTo(24.0)
        }
        
        let seeAllButton = UIButton(type: .custom)
        seeAllButton.setTitleColor(.blue, for: .normal)
        seeAllButton.setTitle("See All", for: .normal)
        seeAllButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        headerView.addSubview(seeAllButton)
        seeAllButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(topicTitleLabel.snp.bottom).offset(0.0)
            make.right.equalTo(-20.0)
            make.width.equalTo(72.0)
            make.height.equalTo(22.0)
        }
        
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.flowLayout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        self.contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.bottom).offset(0.0)
            make.bottom.left.right.equalTo(0.0)
        }
        collectionView.register(TopicCollectionViewCell.self, forCellWithReuseIdentifier: GameTopicCollectionCellIdentifier)
    }
    
    // UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GameTopicCollectionCellIdentifier, for: indexPath) as! TopicCollectionViewCell
        cell.updateClick = {[weak self](button) in
            self!.updateItemClick(button: button)
        }
        cell.model = data[indexPath.row]
        return cell
    }
    
    // UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("indexPath == \(indexPath.row)");
    }
    
    //
    func updateItemClick(button:UIButton) -> Void {
        let cell = button.superview?.superview
        let indexPath = collectionView.indexPath(for: cell as! UICollectionViewCell)
        print("updateItemClick - \(indexPath!.row)")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
